package com.TT.common.REST;

import lombok.Data;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

/**
 * Created by 苏文广 on 2018/11/22
 */
@Data
public class RESTResponse<T> {
    private int code;
    private String msg;
    private T data;

    /**
     * 创建简单成功响应
     * @return RESTResponse
     */
    @NotNull
    @Contract(" -> new")
    public static RESTResponse simple_success(){

        return new RESTResponse(ResCode.Simple_SUCESS.code,ResCode.Simple_SUCESS.msg);
    }

    /**
     * 创建简单失败响应
     * @return RESTResponse
     */
    @NotNull
    @Contract(" -> new")
    public static RESTResponse simple_fail(){
        return new RESTResponse(ResCode.Simple_FAIL.code,ResCode.Simple_FAIL.msg);
    }

    /**
     * 创建带消息的成功响应
     * @param msg 失败消息体string
     * @return RESTResponse
     */
    @NotNull
    @Contract("_ -> new")
    public static RESTResponse msg_Success(String msg){
        return new RESTResponse(ResCode.Simple_SUCESS.code,msg);
    }

    /**
     * 创建带消息的失败响应
     * @param msg 失败消息体string
     * @return RESTResponse
     */
    @NotNull
    @Contract("_ -> new")
    public static RESTResponse msg_Fail(String msg){
        return  new RESTResponse(ResCode.Simple_FAIL.code,msg);
    }

    public RESTResponse() {
    }

    /**
     * 构造方法，需要传入ResCode枚举，和泛型data数据
     * @param resCode 枚举类
     * @param data 数据类
     */
    public RESTResponse(@NotNull ResCode resCode, T data) {
        this.code = resCode.code;
        this.msg = resCode.msg;
        this.data = data;
    }

    public RESTResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private RESTResponse(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

}

